﻿using FreeImageAPI;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace topng
{
    class @ref
    {
        void do1()
        {
            FreeImageAPI.FreeImageBitmap freeImageBitmap = null;

            IntPtr intPtr = IntPtr.Zero;
            Bitmap bitmap = new Bitmap(freeImageBitmap.Width, freeImageBitmap.Height);
            double power = 2.0;
            double logpower = Math.Log(power);
            for (int i = 0; i < freeImageBitmap.Height; i++)
            {
                for (int j = 0; j < freeImageBitmap.Width; j++)
                {
                    float r = Program.ReadFormPtr(intPtr);
                    intPtr += 4;
                    float g = Program.ReadFormPtr(intPtr);
                    intPtr += 4;
                    float b = Program.ReadFormPtr(intPtr);
                    intPtr += 4;

                    float max = Math.Max(r, Math.Max(g, b));
                    int alpha = (int)Math.Ceiling(Math.Log((double)max) / logpower);
                    if ((float)alpha < -128f)
                    {
                        alpha = -128;
                    }
                    if ((float)alpha > 127f)
                    {
                        alpha = 127;
                    }
                    float talpha = (float)Math.Pow(power, (double)alpha);
                    if (r > 0f && r <= 1f)
                    {
                        r = r * 255f / talpha;
                    }
                    else if (r > 1f)
                    {
                        r = 255f / talpha;
                    }
                    else
                    {
                        r = 0f;
                    }
                    if (g > 0f && g <= 1f)
                    {
                        g = g * 255f / talpha;
                    }
                    else if (g > 1f)
                    {
                        g = 255f / talpha;
                    }
                    else
                    {
                        g = 0f;
                    }
                    if (b > 0f && b <= 1f)
                    {
                        b = b * 255f / talpha;
                    }
                    else if (b > 1f)
                    {
                        b = 255f / talpha;
                    }
                    else
                    {
                        b = 0f;
                    }
                    alpha += 128;
                    Color color = Color.FromArgb(alpha, (int)r, (int)g, (int)b);
                    bitmap.SetPixel(j, freeImageBitmap.Height - i - 1, color);
                }
            }
        }

    }
}
